<?php
/*
 * Created on Jul 3, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php

define('FEUSERINFO', 'FEUSERINFO');
define('FEUSERREFER', 'FEUSERREFER');

class Base extends Controller {

	var $_layout = null;
	var $_nodeid = 1;
	var $_external_link = null;
	var $_linkurl = null;
	var $_cid = null;
	var $_setting = null;
	var $_lang_support = null;
	var $_lang_current = null;
	var $_option = null;
	var $_keyword = null;


	function Base() {
		parent::Controller();
		$this->_layout = 'layout/main_layout.php';
		$this->load->model('Model_setting', 'model_setting');
		$this->_setting = $this->model_setting->first_row();

		$this->_external_link = $this->config->item('external_link');
		$keyword = isset($_POST['keyword'])?$_POST['keyword']:"";
		$_SESSION['keyword'] = $keyword;
		$this->_keyword = $_SESSION['keyword'];


		// flush cache
		if (isset ($_GET['flush'])){
			$this->cache->delete_all();
		}
	}

	function render($view, $data) {
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		$language_support = unserialize($this->_setting['language_support']);
		$this->_lang_support = $this->get_language($language_support);
		$this->_option = $this->loadOption();

		$userinfo = null;
		if(isset($_SESSION[FEUSERINFO])){
			$userinfo = $_SESSION[FEUSERINFO];
		}
		$this->load->model('Model_nav', 'model_nav');
		$nav = $this->processNav();
		$footer = $this->processNavFooter();
		$node = $this->processNode($this->_nodeid);
		if(empty($node)){
			$this->_nodeid = 1;
			$node = $this->processNode($this->_nodeid);
		}
		else{
			if(!empty($node['layout']))
				$this->_layout = 'layout/'.$node['layout'];
		}

		$templates = $this->processTemplates($this->_nodeid);

		if($node['target_link']!=''){
			$arr_node = explode('/', $node['target_link']);
			$link_node = $arr_node[count($arr_node)-1];
			$node = $this->processNode($link_node);
			$templates = $this->processTemplates($node['id']);
		}

		if (is_null($data)) {
			$data = array (
				'node' => $node
			);
		}
		$data['templates'] = $templates;
		$data['linkurl'] = $this->_linkurl;

		$content = $this->load->view($view, $data, true);
		return $this->load->view($this->_layout, array (
			'userinfo' => $userinfo,
			'content' => $content,
			'setting' => $this->_setting,
			'lang_support' => $this->_lang_support,
			'lang_current' => $this->_lang_current,
			'option' => $this->_option,
			'nav' => $nav,
			'footer'=> $footer,
			'node' => $node,
			'templates' => $templates,
			'external_link' => $this->_external_link,
			'keyword' => $this->_keyword
		));
	}

	/**
	 *
	 *
	 */
	function renderdesc($view, $data){
		$this->_lang_current = $_SESSION[LANGUAGE_CURRENT];
		$language_support = unserialize($this->_setting['language_support']);
		$this->_lang_support = $this->get_language($language_support);
		$this->_option = $this->loadOption();

		$this->load->model('Model_nav', 'model_nav');
		$nav = $this->processNav();
		$node = $this->processNode($this->_nodeid);
		$template = $this->processTemplate($this->_cid);

		if (is_null($data)) {
			$data = array (
				'node' => $node,
				'nav' => $nav
			);
		}
		$data['template'] = $template;
		$data['linkurl'] = $this->_linkurl;
		$content = $this->load->view($view, $data, true);
		$footer = $this->processNavFooter();

		return $this->load->view($this->_layout, array (
			'content' => $content,
			'setting' => $this->_setting,
			'lang_support' => $this->_lang_support,
			'lang_current' => $this->_lang_current,
			'option' => $this->_option,
			'nav' => $nav,
			'node' => $node,
			'template' => $template,
			'footer' => $footer,
			'external_link' => $this->_external_link
		));
	}

	/**
	 *
	 *
	 */
	function processNav() {
		return $this->model_nav->fetchNav();
	}

	function processNavFooter(){
		return $this->model_nav->fetchNav(0,'sort','asc',null,null,1);
	}

	/**
	 *
	 *
	 */
	function processNode($node_id) {
		$data = $this->cache->get('processNode'.$node_id);
		if(empty($data) && intval($node_id)){
			$data = $this->model_nav->find($node_id);
			$this->cache->write($data, 'processNode'.$this->_nodeid);
		}
		return $data;
	}

	/**
	 *
	 *
	 */
	function processFooter($footerId){
		$data = $this->cache->get('processFooter'.$footerId);
		if(empty($data)){
			$level = 2;
			$view = 1;
			$front_end_display = null;
			$data = $this->model_nav->fetchNav($level, $view, $front_end_display, $footerId,null,1);
			$this->cache->write($data, 'processFooter'.$footerId);
		}
		return $data;
	}

	/**
	 *
	 *
	 */
	function processTemplates($node_id) {
		$this->load->model('Model_c1', 'model_c1');
		$data = $this->cache->get('processTemplates'.$node_id);
		if(empty($data) and intval($node_id)){
			$data = $this->model_c1->find_by_nodeid($node_id);
			$this->cache->write($data, 'processTemplates'.$node_id);
		}
		return $data;
	}

	/**
	 *
	 *
	 */
	function processTemplate($id) {
		$result = null;
		if(intval($id)){
			$this->load->model('Model_c1', 'model_c1');
			$result = $this->model_c1->find_by_id($id);
		}
		return $result;
	}

	/**
	 *
	 *
	 */
	function loadOption(){
		$result = $this->cache->get('loadOption');
		if(empty($data)){
			$this->load->model('Model_option', 'model_option');
			$data = $this->model_option->find_all();
			$result = array();
			if(is_array($data)){
				foreach($data as $item){
					$result[$item['name']] = $item['value'];
				}
				$this->cache->write($result, 'loadOption');
			}
		}
		return $result;
	}

	/**
	 *
	 *
	 *
	 */
	function checkPermission()
	{
		// Get method
		$config = $this->config->item('permission');
		$exclude_methods = $config['exclude']['method'];

		if(isset($_GET['m']) && !empty($_GET['m']))
		{
			$key = array_search($_GET['m'], $exclude_methods); // Find key of given value
			if ($key != NULL || $key !== FALSE) {
			}
			else{
				// Check require permission
				if(intval($this->_nodeid)){
					$node = $this->processNode($this->_nodeid);
					$act = $this->config->item('site_permission');
					if(!empty($node) && ($node['act'] == $act)){
						// Check is logged
						if(session_is_registered(FEUSERINFO)){
							return true;
						}
						else{
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	/**
	 *
	 *
	 * @return
	 */
	function get_language($ids) {
		$this->load->model('Model_country', 'model_country');
		$countries = $this->model_country->find_by_ids($ids);
		//print_r($countries);
		return $countries;
	}
}
?>